package android.slc.utils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author slc
 * @date 2023/10/24
 **/
public class SlcReflexUtils {

    public static <T> Class<T> getGenericSuperclass(Class<?> targetClass) {
        return getGenericSuperclass(targetClass, 0, null);
    }

    public static <T> Class<T> getGenericSuperclass(Class<?> targetClass, int actualTypeIndex) {
        return getGenericSuperclass(targetClass, actualTypeIndex, null);
    }

    public static <T> Class<T> getGenericSuperclass(Class<?> targetClass, Class<?> defGenericSuperclass) {
        return getGenericSuperclass(targetClass, 0, defGenericSuperclass);
    }

    public static <T> Class<T> getGenericSuperclass(Class<?> targetClass, int actualTypeIndex, Class<?> defGenericSuperclass) {
        Type type = targetClass.getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            return (Class<T>) ((ParameterizedType) type).getActualTypeArguments()[actualTypeIndex];
        } else {
            //如果没有指定泛型参数，则默认使用BaseViewModel
            return (Class<T>) defGenericSuperclass;
        }
    }
}
